package org.test.client.ui.left;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JComponent;

import org.test.client.listener.ClickAction;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIConstants;
import org.test.client.ui.util.UIUtil;

/**
 * @author Guodeqi
 *
 */
public class LeftMenuItem extends JComponent{
	public boolean isSingleItem = false;
	
	public static Color normalColor = Icons.getColor("ffffff");
	public static Color rolloverColor = Icons.getColor("eeeeee");
	public static Color selectedColor = Icons.getColor("2fa4e7");
	public static Color lineColor = Icons.getColor("ececec");
	
	public Font txtFont = new Font(Font.DIALOG,Font.PLAIN,15);
	public Color fontColor = Icons.getColor("2fa4e7");
	public Color fontSelectedColor = Icons.getColor("ffffff");
	public int iconSize = 22;
	
	//多级 菜单 设置偏移
	private int marginleft = 0;
	private int rolloverOffset = 8;
	private int diameter = 10;
	
	private int defaultw = 230; 
	private int defaulth = 40; 
	
	public LeftMenuItem thisItem = this;
	public String txt = null;
	public BufferedImage icon = null;
	public BufferedImage iconSelected = null;
	public String iconStrDesc = null;
	public int index = 1;
	
	//是否点击
	private boolean pressed = false;
	//鼠标是否 进入 
	private boolean rollovered = false;

	//点击 以后 背景颜色是否变蓝
	public boolean changeClickUI = true;
	
	private Vector<LeftMenuItem> childs = new Vector<LeftMenuItem>();
	private Vector<ClickAction> actions = new Vector<ClickAction>();
	private LeftMenuBar parentMBar = null;
	
	public LeftMenuItem(String txt){
		this(txt,null,1);
	}
	
	public LeftMenuItem(String txt,BufferedImage icon){
		this(txt,icon,1);
	}
	
	public LeftMenuItem(String txt,int index){
		this(txt,null,index);
	}
	
	public LeftMenuItem(String txt,BufferedImage icon,int index){
		this.txt = txt;
		this.icon = icon;
		this.index = index;
		this.setPreferredSize(new Dimension(defaultw,defaulth));
	}
	
	public LeftMenuItem(String txt,String iconDesc){
		this.txt = txt;
		this.iconStrDesc = iconDesc;
		this.setPreferredSize(new Dimension(defaultw,defaulth));
	}
	
	
	public LeftMenuItem doInit(){
		this.setBackground(normalColor);
//		this.setText(txt);
		this.setFont(txtFont);
		if(iconStrDesc != null ){
			icon = Icons.getImgByDesc(iconStrDesc, iconSize, fontColor);
			iconSelected = Icons.getImgByDesc(iconStrDesc, iconSize, fontSelectedColor);
			tmpImg = icon;
		}
		if(icon != null && iconStrDesc==null){
//			this.setIcon(new ImageIcon(icon));
			iconSelected = icon;
			tmpImg = icon;
		}
		
		this.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent mouseevent) {
				if(!isSingleItem){
					thisItem.setPressedForClick(true);
					if(parentMBar != null){
						parentMBar.setSelectOnItem(thisItem);
					}
					//do something
					execListener(mouseevent);
					return ;
				}
				
				if(!thisItem.getPressed()){
					thisItem.setPressedForClick(true);
				}else{
					thisItem.setPressedForClick(false);
				}
				execListener(mouseevent);
			}
			
			@Override
			public void mouseEntered(MouseEvent mouseevent) {
				if(!thisItem.getPressed()){
					thisItem.setRollovered(true);
				}
				thisItem.setCursor(UIConstants.handCursor);
			}
			
			@Override
			public void mouseExited(MouseEvent mouseevent) {
				thisItem.setRollovered(false);
				thisItem.setCursor(UIConstants.defaultCursor);
			}
		});
		
		this.setPreferredSize(new Dimension(defaultw,defaulth));
		this.setSize(defaultw, defaulth);
		marginleft = (index-1) * rolloverOffset;
		return this;
	}
	
	Color tmpBack = normalColor;
	BufferedImage tmpImg = null;
	Color tmpFontColor = fontColor;
	int nowOffest = rolloverOffset*(index-1);
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		
		if(index == 1 && childs.size()==0){
			g2d.setColor(lineColor);
			g2d.drawLine(0, 0, defaultw, 0);
			g2d.drawLine(0, defaulth-1, defaultw, defaulth-1);
		}
		
		if(pressed || rollovered){
			g2d.setColor(tmpBack);
			g2d.fillRoundRect(nowOffest+marginleft, 1,defaultw-2-nowOffest-marginleft, defaulth-2,diameter,diameter);
		}
		
		int x = nowOffest + 5 + marginleft;
		int y = 0;
		if(tmpImg != null){
			y = (defaulth-tmpImg.getHeight())/2;
			g2d.drawImage(tmpImg, x, y, tmpImg.getWidth(),tmpImg.getHeight(),null);
			x = x+tmpImg.getWidth();
		}
		
		g2d.setColor(tmpFontColor);
		g2d.setFont(txtFont);
		FontMetrics fm = g.getFontMetrics();
		int ascent = fm.getAscent();
		int descent = fm.getDescent();
		
		y = defaulth/2 + (ascent-descent)/2;
		g2d.drawString(txt, x, y);
		
	}

	public boolean getPressed() {
		return pressed;
	}

	public void setNormal(){
		tmpImg = icon;
		tmpFontColor = fontColor;
		tmpBack = normalColor;
		pressed = false;
		rollovered = false;
		OffsetRun offsetRun = new OffsetRun(thisItem,false);
		new Thread(offsetRun).start();
	}
	
	public void setPressedForClick(boolean pressed) {
		if(!changeClickUI){
			return;
		}
		if(this.pressed != pressed){
			this.pressed = pressed;
			if(pressed){
				tmpBack = selectedColor;
				tmpImg = iconSelected;
				tmpFontColor = fontSelectedColor;
			}else{
				tmpImg = icon;
				tmpFontColor = fontColor;
				tmpBack = rolloverColor;
				rollovered = true;
			}
			this.repaint();
		}
	}

	public boolean getRollovered() {
		return rollovered;
	}

	public void setRollovered(boolean rollovered) {
		if(this.rollovered != rollovered){
			this.rollovered = rollovered;
			if(!pressed && rollovered){
				tmpBack = rolloverColor;
			}
			if(pressed){
				return;
			}
			OffsetRun offsetRun = new OffsetRun(thisItem,rollovered);
			new Thread(offsetRun).start();
		}
	}

	public int getNowOffest() {
		return nowOffest;
	}

	public void setNowOffest(int nowOffest) {
		if(nowOffest != this.nowOffest){
			this.nowOffest = nowOffest;
			this.repaint();
		}
	}

	public int getRolloverOffset() {
		return rolloverOffset;
	}

	public void setRolloverOffset(int rolloverOffset) {
		this.rolloverOffset = rolloverOffset;
	}
	
	public void setMenuBar(LeftMenuBar mbar){
		this.parentMBar = mbar;
	}

	public int getDefaultw() {
		return defaultw;
	}

	public void setDefaultw(int defaultw) {
		this.defaultw = defaultw;
	}

	public int getDefaulth() {
		return defaulth;
	}

	public void setDefaulth(int defaulth) {
		this.defaulth = defaulth;
	}

	public Vector<LeftMenuItem> getChilds() {
		return childs;
	}

	public void setChilds(Vector<LeftMenuItem> childs) {
		this.childs = childs;
	}
	
	public void addChild(LeftMenuItem item){
		childs.add(item);
	}
	
	public void addChild(LeftMenuItem ... menus){
		for(int b=0;b<menus.length;b++){
			childs.add(menus[b]);
		}
	}

	public LeftMenuBar getMenuBar(){
		if(parentMBar != null){
			return parentMBar;
		}else{
			int count = 10;
			Container p = this.getParent();
			if(p instanceof LeftMenuBar){
				return (LeftMenuBar)p;
			}
			for(int i =0;i<count;i++){
				p = p.getParent();
				if(p instanceof LeftMenuBar){
					return (LeftMenuBar)p;
				}
			}
		}
		return null;
	}
	
	public void execListener(MouseEvent mouseevent){
		if(actions != null && actions.size()>0){
			for(ClickAction a : actions){
				a.doOnClick(mouseevent);
			}
		}else{
//			System.out.println("Action is null");
		}
	}
	
	public void addClickListener(ClickAction action){
		if(action != null){
			actions.add(action);
		}
	}

	public String getTxt() {
		return txt;
	}

	public void setTxt(String txt) {
		this.txt = txt;
	}
	
}

